<template>
  <!-- eslint-disable vue/html-closing-bracket-newline -->
  <!--eslint-disable vue/html-indent  -->
  <!-- eslint-disable vue/html-self-closing -->
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect :show="scene !== 0" @getCategoryId="getCategoryId" />
    </el-card>
    <el-card>
      <div v-show="scene === 0">
        <!--SPU列表的结构-->
        <el-button
          :disabled="!category3Id"
          icon="el-icon-plus"
          type="primary"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table :data="records" border style="width: 100%; margin-top: 10px">
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="80"
          />
          <el-table-column label="spu名称" prop="spuName" />
          <el-table-column label="spu描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                type="success"
                @click="addSku(row)"
              />
              <el-button
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                type="warning"
                @click="updateSpu(row)"
              />
              <el-button
                icon="el-icon-info"
                size="mini"
                title="查看当前spu的sku列表"
                type="info"
                @click="handler(row)"
              />
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="deleteSpu(row)"
              >
                <el-button
                  slot="reference"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  type="danger"
                  style="margin-left: 10px"
                />
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-size="limit"
          :page-sizes="[3, 5, 10]"
          :total="total"
          layout="prev, pager, next, jumper,->,sizes,total"
          style="text-align: center; margin-top: 10px"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        />
      </div>
      <SpuForm v-show="scene === 1" ref="spu" @changeScene="changeScene" />
      <SkuForm v-show="scene === 2" ref="sku" @changeScenes="changeScenes" />
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的SKU列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table v-loading="loading" border style="width: 100%" :data="skuList">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格" prop="price" />
        <el-table-column label="重量" prop="weight" />
        <el-table-column label="默认图片">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
/* eslint-disable comma-dangle */
/* eslint-disable quotes */
/* eslint-disable semi */
import SpuForm from "@/views/product/Spu/SpuForm";
import SkuForm from "@/views/product/Spu/SkuForm";

export default {
  name: "Spu",
  components: {
    SpuForm,
    SkuForm,
  },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1,
      limit: 5,
      records: [], // spu列表的数据
      total: 0,
      scene: 0, // 0代表SPU列表数据 1代表添加、修改SPU 2代表添加SKU
      dialogTableVisible: false, //控制对话框的显示与隐藏
      spu: {},
      skuList: [], //SKU列表的数据
      loading: true,
    };
  },
  methods: {
    // 获取SPU列表数据的方法
    async getSpuList(page = 1) {
      this.page = page;
      await this.$API.spu
        .reqSpuList(this.page, this.limit, this.category3Id)
        .then((result) => {
          if (result.code === 200) {
            this.total = result.data.total;
            this.records = result.data.records;
          }
        });
    },
    // 三级联动
    getCategoryId(data) {
      if (data.level === 1) {
        this.category1Id = data.categoryId;
        this.category2Id = "";
        this.category3Id = "";
        this.getSpuList();
      } else if (data.level === 2) {
        this.category2Id = data.categoryId;
        this.category3Id = "";
        this.getSpuList();
      } else {
        this.category3Id = data.categoryId;
        // 获取SPU列表数据
        this.getSpuList();
      }
    },
    // 分页器某一页展示数据的条数
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    // 添加SPU按钮的回调
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    // 修改某一个SPU
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    // SpuForm自定义事件回调
    changeScene({ scene, flag }) {
      this.$on("changeScene", (this.scene = scene));
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    //删除SPU
    async deleteSpu(row) {
      await this.$API.spu.reqDeleteSpu(row.id).then((result) => {
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
        }
      });
    },
    //添加SKU
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //skuForm通知父组件修改场景
    changeScenes(scene) {
      this.scene = scene;
    },
    //查看SKU的按钮
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = spu;
      await this.$API.spu.reqSkuList(spu.id).then((result) => {
        if (result.code == 200) {
          this.skuList = result.data;
          this.loading = false;
        }
      });
    },
    //关闭对话框的回调
    close(done) {
      this.loading = true;
      done();
      this.skuList = [];
    },
  },
};
</script>

<style></style>
